home *** CD-ROM | disk | FTP | other *** search
/ Enter 2006 September / Enter 09 2006.iso / Internet / SpamExperts Home 1.1 / SpamExperts Home.exe / lib / spamexperts.modules / ZODB / utils.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2006-07-14  |  5.7 KB  |  193 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. import sys
  5. import time
  6. import struct
  7. from struct import pack, unpack
  8. from binascii import hexlify
  9. import cPickle as pickle
  10. from cStringIO import StringIO
  11. import weakref
  12. import warnings
  13. from persistent.TimeStamp import TimeStamp
  14. __all__ = [
  15.     'z64',
  16.     'p64',
  17.     'u64',
  18.     'U64',
  19.     'cp',
  20.     'newTimeStamp',
  21.     'oid_repr',
  22.     'serial_repr',
  23.     'tid_repr',
  24.     'positive_id',
  25.     'readable_tid_repr',
  26.     'WeakSet',
  27.     'DEPRECATED_ARGUMENT',
  28.     'deprecated37',
  29.     'deprecated38',
  30.     'get_pickle_metadata']
  31. DEPRECATED_ARGUMENT = object()
  32.  
  33. def deprecated37(msg):
  34.     warnings.warn('This will be removed in ZODB 3.7:\n%s' % msg, DeprecationWarning, stacklevel = 3)
  35.  
  36.  
  37. def deprecated38(msg):
  38.     warnings.warn('This will be removed in ZODB 3.8:\n%s' % msg, DeprecationWarning, stacklevel = 3)
  39.  
  40. z64 = '\x00' * 8
  41. if not sys.hexversion >= 33751040:
  42.     raise AssertionError
  43.  
  44. def p64(v):
  45.     '''Pack an integer or long into a 8-byte string'''
  46.     return pack('>Q', v)
  47.  
  48.  
  49. def u64(v):
  50.     '''Unpack an 8-byte string into a 64-bit long integer.'''
  51.     return unpack('>Q', v)[0]
  52.  
  53. U64 = u64
  54.  
  55. def cp(f1, f2, l):
  56.     read = f1.read
  57.     write = f2.write
  58.     n = 8192
  59.     while l > 0:
  60.         if n > l:
  61.             n = l
  62.         
  63.         d = read(n)
  64.         if not d:
  65.             break
  66.         
  67.         write(d)
  68.         l = l - len(d)
  69.  
  70.  
  71. def newTimeStamp(old = None, TimeStamp = TimeStamp, time = time.time, gmtime = time.gmtime):
  72.     t = time()
  73.     ts = TimeStamp(gmtime(t)[:5] + (t % 60,))
  74.     if old is not None:
  75.         return ts.laterThan(old)
  76.     
  77.     return ts
  78.  
  79.  
  80. def oid_repr(oid):
  81.     if isinstance(oid, str) and len(oid) == 8:
  82.         as_hex = hexlify(oid).lstrip('0')
  83.         if len(as_hex) & 1:
  84.             as_hex = '0' + as_hex
  85.         elif as_hex == '':
  86.             as_hex = '00'
  87.         
  88.         return '0x' + as_hex
  89.     else:
  90.         return repr(oid)
  91.  
  92. serial_repr = oid_repr
  93. tid_repr = serial_repr
  94.  
  95. def readable_tid_repr(tid):
  96.     result = tid_repr(tid)
  97.     if isinstance(tid, str) and len(tid) == 8:
  98.         result = '%s %s' % (result, TimeStamp(tid))
  99.     
  100.     return result
  101.  
  102. _ADDRESS_MASK = 256 ** struct.calcsize('P')
  103.  
  104. def positive_id(obj):
  105.     '''Return id(obj) as a non-negative integer.'''
  106.     result = id(obj)
  107.     if result < 0:
  108.         result += _ADDRESS_MASK
  109.         if not result > 0:
  110.             raise AssertionError
  111.     
  112.     return result
  113.  
  114.  
  115. def get_pickle_metadata(data):
  116.     if data.startswith('(c'):
  117.         global_prefix = 2
  118.     elif data.startswith('c'):
  119.         global_prefix = 1
  120.     else:
  121.         global_prefix = 0
  122.     if global_prefix:
  123.         (modname, classname, rest) = data.split('\n', 2)
  124.         modname = modname[global_prefix:]
  125.         return (modname, classname)
  126.     
  127.     f = StringIO(data)
  128.     u = pickle.Unpickler(f)
  129.     
  130.     try:
  131.         class_info = u.load()
  132.     except Exception:
  133.         err = None
  134.         print 'Error', err
  135.         return ('', '')
  136.  
  137.     if isinstance(class_info, tuple):
  138.         if isinstance(class_info[0], tuple):
  139.             (modname, classname) = class_info[0]
  140.         else:
  141.             (modname, classname) = class_info
  142.     else:
  143.         modname = repr(class_info)
  144.         classname = ''
  145.     return (modname, classname)
  146.  
  147.  
  148. class WeakSet(object):
  149.     """A set of objects that doesn't keep its elements alive.
  150.  
  151.     The objects in the set must be weakly referencable.
  152.     The objects need not be hashable, and need not support comparison.
  153.     Two objects are considered to be the same iff their id()s are equal.
  154.  
  155.     When the only references to an object are weak references (including
  156.     those from WeakSets), the object can be garbage-collected, and
  157.     will vanish from any WeakSets it may be a member of at that time.
  158.     """
  159.     
  160.     def __init__(self):
  161.         self.data = weakref.WeakValueDictionary()
  162.  
  163.     
  164.     def __len__(self):
  165.         return len(self.data)
  166.  
  167.     
  168.     def __contains__(self, obj):
  169.         return id(obj) in self.data
  170.  
  171.     
  172.     def add(self, obj):
  173.         self.data[id(obj)] = obj
  174.  
  175.     
  176.     def remove(self, obj):
  177.         del self.data[id(obj)]
  178.  
  179.     
  180.     def map(self, f):
  181.         for wr in self.as_weakref_list():
  182.             elt = wr()
  183.             if elt is not None:
  184.                 f(elt)
  185.                 continue
  186.         
  187.  
  188.     
  189.     def as_weakref_list(self):
  190.         return self.data.data.values()
  191.  
  192.  
  193.